<?php

/** 
 * 预付款日志模型(转入,转出,买水)
 * 
 */
class Prepayment_log_mdl extends MY_Model {
	public function __construct() {
		parent::__construct ();
		$this->_table = 'hs_prepayment_log';
	}
	private $userID = NULL;
	/**
	 * 设置用户ID
	 * 
	 * @param int $uid        	
	 */
	public function setUserID($uid) {
		$this->userID = $uid;
	}
	/**
	 * 转入到预付款
	 * 
	 * @param int $amount        	
	 * @return boolean
	 */
	public function transferIn($amount) {
		$amount = abs ( $amount );
		return $this->save ( 'IN', $amount );
	}
	/**
	 * 转出到余额
	 * 
	 * @param unknown_type $amount        	
	 * @return boolean
	 */
	public function transferOut($amount,$adminID=0) {
		$amount = - 1 * abs ( $amount );
		return $this->save ( 'OUT', $amount,$adminID );
	}
	/**
	 * 买水消费
	 * 
	 * @param int $amount        	
	 * @return boolean
	 */
	public function buy($amount) {
		$amount = - 1 * abs ( $amount );
		return $this->save ( 'BUY', $amount );
	}
	/**
	 * 保存到数据库
	 * @param enum $type IN/OUT/BUY
	 * @param int $amount
	 * @return boolean
	 */
	private function save($type, $amount,$adminID=0) {
		if (empty ( $this->userID )) {
			trigger_error ( '必须传入用户ID', E_USER_ERROR );
		}
		$insert = array (
				'userID' => $this->userID,
				'type' => $type,
				'time' => time (),
				'amount' => $amount ,
				'adminID'=>$adminID,
		);
		return $this->insert ( $insert );
	}
}

